--
-- Author: gnar
-- Date: 2015-12-31 18:41:58
--
local AdapterSendMsg = class("AdapterSendMsg")
local NetMsg = import(".network.NetMsg")
local gInstance = nil

function AdapterSendMsg.getInstance()
	if not gInstance then
		gInstance = AdapterSendMsg.new()
	end

	return gInstance
end

function AdapterSendMsg:sendMsg(action, data)
	
	if ManagerUserData.loginMode == 1 then  -- 1单机 2联网   loginMode这个的定义又不会经常变化 不需要像word一样枚举
		local backmsg = _responseAction(action, data)
		self:sendCallback(backmsg)
	elseif ManagerUserData.loginMode == 2 then
		ManagerNet:sendCmd(action, data)
	end

end

function AdapterSendMsg:sendCallback(msg)
	if not msg:isOK() then
		printError("Game:netCallback netError:%d", msg:getError())
		return
	end

	local body = msg:getBody()
	local action = msg:getAction()
	printInfo("Game:netCallback action:%s", action)

	--同步数据 post事件
	if "user.login" == action then   
		if ManagerUserData.loginMode == 1 then
			ManagerUserData.userData.characterDataForChoose = body
		elseif ManagerUserData.loginMode == 2 then
			ManagerUserData.userData.characterDataForChoose = body
		end
	elseif "user.creteCharacter" == action then
		if ManagerUserData.loginMode == 1 then
			ManagerUserData.userData.characterDataForChoose = body
		elseif ManagerUserData.loginMode == 2 then
			ManagerUserData.userData.characterDataForChoose = body
		end
	elseif "user.deleteCharacter" == action then
		if ManagerUserData.loginMode == 1 then
			ManagerUserData.userData.characterDataForChoose = body
		elseif ManagerUserData.loginMode == 2 then
			ManagerUserData.userData.characterDataForChoose = body
		end
	elseif "user.enterGame" == action then
		local characterId = body
		local shortLivedData = ManagerCookie.getValue(enumlCookieFlag.shortLivedData) or {}
		shortLivedData["lastCharacterId" .. ManagerUserData.loginMode] = characterId
		ManagerCookie.setValue(enumlCookieFlag.shortLivedData,shortLivedData)
		ManagerUserData.userData.activityCharacterId = characterId

	elseif "chat.newChat" == action then  --这个不论单机 或 联网 只要服务器连上就会收到这个协议的
		ManagerChatData:insertChatItem(body, ManagerChatData.channels.total)  
    	ManagerChatData:insertChatItem(body)  
    	print("test chat.newChat")
	elseif "user.move" == action then

	elseif "user.attack" == action then

	elseif "mob.attack" == action then

	elseif "mob.reborn" == action then

	elseif "mob.dead" == action or "user.dead" == action then

	elseif "play.move" == action then

	elseif "play.dead" == action then

	elseif "play.reborn" == action then
		
	elseif "play.attackMove" == action then

	elseif "play.attack" == action then

	elseif "play.chat" == action then

	end

	ManagerNotification:post(action,body)
end


--返回客户端请求 模拟服务器功能 
_responseAction = function(action, data)

	local msg = NetMsg.new()
	msg:setAction(action)
	if "user.login" == action then  
		local userId = data.userId
		local characterList = ManagerCookie.getValue(enumlCookieFlag.characterDataForChoose)
		msg:setBody(characterList)

	elseif "user.creteCharacter" == action then
		local characterList = ManagerCookie.getValue(enumlCookieFlag.characterDataForChoose) or {}
		characterList[#characterList + 1] = data.characterInfo
		characterList[#characterList].characterId = #characterList
		msg:setBody(characterList)
		ManagerCookie.setValue(enumlCookieFlag.characterDataForChoose,characterList)
	
	elseif "user.deleteCharacter" == action then
		local characterList = ManagerCookie.getValue(enumlCookieFlag.characterDataForChoose) or {}
		table.remove(characterList,data.characterId)
		msg:setBody(characterList)
		ManagerCookie.setValue(enumlCookieFlag.characterDataForChoose,characterList)
		ManagerCookie.setValue(enumlCookieFlag.characterDataForChoose,characterList)

	elseif "user.enterGame" == action then
		msg:setBody(data.characterId)	
	elseif "chat.sendChat" == action then  
		ManagerNet:sendCmd(action, data)  -- 即使单机也可以聊天的
		
	elseif "user.move" == action then

	elseif "user.attack" == action then

	elseif "mob.attack" == action then

	elseif "mob.reborn" == action then

	elseif "mob.dead" == action or "user.dead" == action then

	elseif "play.move" == action then

	elseif "play.dead" == action then

	elseif "play.reborn" == action then
		
	elseif "play.attackMove" == action then

	elseif "play.attack" == action then

	elseif "play.chat" == action then

	end

	return msg
end

return AdapterSendMsg